package com.abe.libcore.base.screen.j;

import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.abe.libcore.base.screen.OnHandlerListener;
import com.abe.libquick.utils.NormalUtils;

import java.util.Date;
import java.util.Locale;

public abstract class AbeJFragment extends BJScreenFragment {
    private final int appToast = 0x999;
    protected View rootView;
    protected boolean isAlive;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {
        isAlive = true;
        return super.onCreateView(inflater, container, savedInstanceState);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        isAlive = false;
    }

    /**********************************************************************************************/
    protected void hideSoftInput() {
        if (getActivity() == null) return;
        InputMethodManager manager = ((InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE));
        if (manager == null || getActivity().getCurrentFocus() == null) return;
        manager.hideSoftInputFromWindow(getActivity().getCurrentFocus().getWindowToken(), 0);
    }

    public String getEmpty(String value) {
        return TextUtils.isEmpty(value) ? "" : value;
    }

    public String getText(EditText editText) {
        if (editText == null) return "";
        return editText.getText().toString().trim();
    }

    public String getText(TextView textView) {
        if (textView == null) return "";
        return textView.getText().toString().trim();
    }

    public Date stringToDate(String dateStr, String rule) {
        return NormalUtils.stringToDate(rule, dateStr);
    }

    public String dateToString(Date date, String rule) {
        return NormalUtils.dateToString(rule, date);
    }

    /**********************************************************************************************/
    protected class BaseHandler extends Handler {
        BaseHandler(Looper looper) {
            super(looper);
        }

        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if (msg.what == appToast) {
                Toast.makeText(getContext(), msg.obj.toString(), Toast.LENGTH_SHORT).show();
            } else {
                if (isImp(AbeJFragment.this.getClass(), OnHandlerListener.class)) {
                    ((OnHandlerListener) AbeJFragment.this).dispatchHandleMessage(msg);
                }
            }
        }
    }

    protected void toast(String content) {
        if (TextUtils.isEmpty(content)) return;
        localSendMsg(appToast, content);
    }

    public void toast(int strRes) {
        toast(getResources().getString(strRes));
    }

    public void toast(String rule, Object... objects) {
        toast(String.format(Locale.CHINA, rule, objects));
    }

    protected void localSendMsg(int what, Object obj) {
        getHandler().sendMessage(getMessage(what, obj));
    }

    public void localEmptyMsg(int what) {
        getHandler().sendEmptyMessage(what);
    }

    //获取Message
    protected Message getMessage(int what, Object obj) {
        Message msg = new Message();
        msg.what = what;
        msg.obj = obj;
        return msg;
    }

    //获取Handler
    protected BaseHandler getHandler() {
        return new BaseHandler(Looper.getMainLooper());
    }

    /**
     * 是否实现接口
     *
     * @param a 类
     * @param b 接口
     */
    protected Boolean isImp(Class<?> a, Class<?> b) {
        return b.isAssignableFrom(a);
    }
}
